using System;
using CStock;
using CUtil;
using System.Diagnostics;
namespace CStock
{

    //[System.Xml.Serialization.XmlRootAttribute("TradeCondition", Namespace="", IsNullable=false)]
    public class TradersFactory : Factory, ITradersFactory
    {

        ITraders traders;

        public ITraders Traders
        {
            get
            {
                return this.traders;
            }
        }
        public TradersFactory()
        {

            
            traders = new Traders();
            traders.ID = "Default";
            this.stockObjectBase = traders as StockObjectBase;
        }

        #region  Members





        #endregion

        public virtual bool Create_TestData(string id)
        {
            this.stockObjectBase.ID = id;
            return Create_TestData();

        }
        public override bool Create_TestData()
        {
            double moneyStart = 100000;
            int number = 1;

            return Create_TestData(moneyStart, number);

        }

        /// - the sum of all the money of a economy is held by all traders
        /// - the state economy is considered as a trader
        /// - a trader can also lend money - not yet implemented
        /// - a trader can have stocks, bunds, and cash money
        /// 
        /// make a sum over all the money, stock value and bunds - as a test
        public bool Create_TestData(double moneyStart, int number)
        {
            //moneyStart = half for intraday - half for daily
            //double moneyStartDaily = moneyStart / numberOfTraders;

            if (number < 1)
                return false;

            
            double moneyLeft = moneyStart;

            //Factory traderFactory = new TraderFactory();
            double moneyPerItem = moneyLeft / number;

            for (int i = 0; i < number; i++)
            {
                ITradeSystem tradeSystem = new TradeSystem();

               
                //the money for each item:

                int randomNumber = Convert.ToInt32(RandomGenerator.Random(1, Convert.ToInt32(moneyPerItem)));



                moneyLeft -= randomNumber;

                TraderFactory tf = new TraderFactory();
                tf.Create_Default();
                ITrader trader = tf.StockObjectBase as ITrader;

                trader.TradeSimulation.TradeSystem = tradeSystem;
                trader.Money_Initial = randomNumber;
                trader.Money_Current = randomNumber;

                traders.Add(trader);


            }
            //the last trader gets also the money left! (so that the sum = economy sum
            //ITrader trader = traders[i];
            //trader.Money_Initial += moneyLeft;
            //trader.Money_Current = trader.Money_Initial;

            //the money left is divided equally between the traders
            moneyPerItem = moneyLeft / number;
            for (int i = 0; i < number; i++)
            {

                ITrader trader = traders[i];

                trader.Money_Initial += moneyPerItem;
                trader.Money_Current = trader.Money_Initial;



            }

            return true;


        }

      
        /// <summary>
        /// attention - new object variable for dataClass!!
        /// </summary>
        /// <returns></returns>
        public override bool DeSerialize()
        {
            if (base.DeSerialize())
            {
                this.traders = this.stockObjectBase as ITraders;
                return true;
            }

            return false; 

        }

    }
}
